//
//  TreeViewController.m
//  AlgorithmDemo
//
//  Created by zxs on 2020/5/11.
//  Copyright © 2020 zxs. All rights reserved.
//

#import "TreeViewController.h"
#import "BinaryTree.h"

@interface TreeViewController ()

// 二叉搜索树的根节点，代表一棵树
@property (strong, nonatomic) BinaryTreeNode *rootNode;

@end

@implementation TreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

// 创建树
- (IBAction)createButtonTouched:(id)sender {
    NSArray *values = @[@200, @23, @456, @89, @23, @670, @5674, @15];
    self.rootNode = [BinaryTree createTreeWithValues:values];
}

// 先序遍历
- (IBAction)preOrderButtonTouched:(id)sender {
    NSMutableArray<NSNumber *> *preArray = [NSMutableArray array];
    [BinaryTree preOrderTraverseTree:self.rootNode handler:^(BinaryTreeNode * _Nonnull treeNode) {
        [preArray addObject:[NSNumber numberWithInteger:treeNode.value]];
    }];
    NSLog(@"先序遍历：%@", preArray);
}

// 中序遍历
- (IBAction)inOrderButtonTouched:(id)sender {
    NSMutableArray<NSNumber *> *inArray = [NSMutableArray array];
    [BinaryTree inOrderTraverseTree:self.rootNode handler:^(BinaryTreeNode * _Nonnull treeNode) {
        [inArray addObject:[NSNumber numberWithInteger:treeNode.value]];
    }];
    NSLog(@"中序遍历：%@", inArray);
}

// 后序遍历
- (IBAction)postOrderButtonTouched:(id)sender {
    NSMutableArray<NSNumber *> *postArray = [NSMutableArray array];
    [BinaryTree postOrderTraverseTree:self.rootNode handler:^(BinaryTreeNode * _Nonnull treeNode) {
        [postArray addObject:[NSNumber numberWithInteger:treeNode.value]];
    }];
    NSLog(@"后序遍历：%@", postArray);
}

- (IBAction)invertButtonTouched:(id)sender {
    [BinaryTree invertBinaryTree:self.rootNode];
}


@end
